package com.qfedu.controller;

import com.qfedu.service.UserService;
import com.qfedu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiJH
 * @date 2023-01-31
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    //跳转页面
    @RequestMapping("/{page}")
    public String toPage(@PathVariable("page") String page){
        System.out.println(page);
        return page;
    }
    //添加用户
    @PostMapping("addUser")
    public String addUser(User user) {
        userService.addUser(user);
        return "success";
    }
    //查询全部用户
    @RequestMapping("findAll")
    public String listUser(Model model){
        List<User> list = userService.listUser();
        model.addAttribute("list", list);
        return "list";
    }

    /**
     * 防止favico报错
     */
    @RequestMapping("/favicon.ico")
    @ResponseBody
    void getFav() {
    }

    /**
     * 根据id删除
     */
    @PostMapping("deleteUser")
    public String deleteUser(int id) {
        userService.deleteUser(id);
        return "success";
    }
    @PostMapping("updateUser")
    public String updateUser(User user) {
        userService.updateUser(user);
        return "success";
    }
}